{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55438090-5bca-4eb4-8f6f-c6be8ad035ab",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 37.PIR 人体热释电感应模块实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：37.PIR 人体热释电感应模块实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：PIR 人体热释电感应模块实验\n",
    "直流电机风扇模块实验，通过GPIOzero 自带的 MotionSensor 人体热释电感应模块库，可以方便感知运动物体的释放出的热释电！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7cba9f96-1e39-4c9b-8f3a-75f2f40bc474",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import RGBLED, MotionSensor\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.定义PIR 和RGB 对应的管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_rgb = RGBLED(red=18, green=19, blue=27)  # 定义RGB灯管脚\n",
    "makerobo_pir = MotionSensor(17)                   # 定义PIR人体热释电管脚"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49b9235a-0ecd-4e59-bae2-dbb73493c916",
   "metadata": {},
   "source": [
    "## 3.定义循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cdf51c59-79a3-4e8b-b4d5-b8dbc1ea1fdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    try:\n",
    "        while True:\n",
    "            if makerobo_pir.motion_detected:  # 检查PIR传感器检测到的运动\n",
    "                makerobo_rgb.color = (1, 1, 0)         # 设置LED颜色为黄色(红色+绿色)\n",
    "            else:\n",
    "                makerobo_rgb.color = (0, 0, 1)         # 设置LED颜色为蓝色(仅为蓝色)\n",
    "            sleep(0.1)                        # 短延迟，降低CPU负载\n",
    "    \n",
    "    except KeyboardInterrupt:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "    try:\n",
    "        makerobo_loop()  # 循环函数\n",
    "    except KeyboardInterrupt:   # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "        pass  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db9098b3-6f16-4178-b433-d5bb9e939a16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
